/**
 * 
 */
package controller;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import adapter.SettingAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.TwoLineListItem;

import com.actionbarsherlock.app.SherlockFragment;
import com.example.remotecontrol.R;

import common.DimensionHelper;
import components.FragmentMediator;
import components.PasscodeManager;
import components.SMSCommandManager;

/**
 * @author User
 * 
 */
public class PasscodeFragment extends SherlockFragment implements
		OnItemSelectedListener {

	private ListView mListView;
	private ArrayList<String[]> mListItem = new ArrayList<String[]>();
	ArrayAdapter adapter = null;

	/**
	 * 
	 */
	public PasscodeFragment() {
		// TODO Auto-generated constructor stub\

		// mListItem.add(new String[] { "Remove Passcode", ""});

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.setting_fragment, container,
				false);

		mListView = (ListView) view.findViewById(R.id.list_view);

		adapter = new SettingAdapter(getActivity(), mListItem);

		mListView.setAdapter(adapter);

		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if (arg3 == 0) {
					// TODO: Push to Fragment Set text
					if (PasscodeManager.sharedPasscode(getActivity()).getPassCode() == null) {
					Fragment passcode = FragmentMediator
							.CreateFragment(FragmentMediator.FRAGMENT_SET_PASSCODE);
					FragmentMediator.transitView(PasscodeFragment.this,
							passcode, null, true);
					} else {
						boolean isExpiredSession = PasscodeManager.sharedPasscode(PasscodeFragment.this.getActivity())
								.isExpiredSession();

						if (isExpiredSession) {
						Intent newIntent = new Intent(PasscodeFragment.this.getActivity(), PasscodeActivity.class);
						newIntent.setAction(TabNavigation.ACTION_CHANGE_PASSCODE);
						startActivityForResult(newIntent, 0);
						} else {
							Fragment passcode = FragmentMediator
									.CreateFragment(FragmentMediator.FRAGMENT_SET_PASSCODE);
							List<Fragment> fragment = PasscodeFragment.this.getActivity().getSupportFragmentManager()
									.getFragments();

							FragmentMediator.transitView(
									fragment.get(fragment.size() - 1), passcode, null, true);
						}
					}
				} else if (arg3 == 1) {
					boolean isExpiredSession = PasscodeManager.sharedPasscode(PasscodeFragment.this.getActivity())
							.isExpiredSession();

					if (isExpiredSession) {
					Intent newIntent = new Intent(PasscodeFragment.this.getActivity(), PasscodeActivity.class);
					newIntent.setAction(TabNavigation.ACTION_CLEAR_PASSCODE);
					startActivityForResult(newIntent, 0);
					} else {
						PasscodeManager.sharedPasscode(PasscodeFragment.this.getActivity()).clearPasscode(PasscodeFragment.this.getActivity());
						Toast.makeText(PasscodeFragment.this.getActivity(),
								R.string.msg_clear_passcode_successfully,
								Toast.LENGTH_SHORT).show();
						
						onResume();
					}
//					PasscodeManager.sharedPasscode(getActivity())
//							.clearPasscode(getActivity());
//					Toast.makeText(getActivity(),
//							R.string.msg_clear_passcode_successfully,
//							Toast.LENGTH_SHORT).show();
				}
			}
		});
		return view;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getView().findViewById(R.id.root_layout).setPadding(
				0, DimensionHelper.getPxFromDp(getActivity(), 50), 0, 0);
		mListItem.clear();
		
		if (PasscodeManager.sharedPasscode(getActivity()).getPassCode() == null)	{
			mListItem
			.add(new String[] { this.getString(R.string.set_passcode), "" });
		} else {
			mListItem
			.add(new String[] { this.getString(R.string.change_passcode), "" });
		}
		
		mListItem.add(new String[] { this.getString(R.string.clear_passcode),
				"" });

		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
	}
}
